package org.aom.litebean.module.menu;

import java.util.List;
import java.util.Map;

import org.aom.entity.menu.Menu;
import org.aom.entity.menu.ModuleItem;
import org.aom.litebean.module.base.BeforeRenderBaseBean;
import org.aom.service.menu.IMenuManager;
import org.aom.service.menu.IModuleItemManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridDataProvider;
import org.operamasks.faces.util.Browser;

@ManagedBean(name = "module.menu.menuLinkModuleItemBean", scope = ManagedBeanScope.SESSION)
public class MenuLinkModuleItemBean extends BeforeRenderBaseBean {

	private Long selectedModuleId;

	private Menu selectedMenu;

	public static String SELECTED_MODULE_ID = "selectedModuleId";

	@Bind(id = "uncheckedDataGrid", attribute = "binding")
	private UIDataGrid uncheckedDataGrid;

	@Bind(id = "checkedDataGrid", attribute = "binding")
	private UIDataGrid checkedDataGrid;

	@ManagedProperty("#{menuManager}")
	private IMenuManager menuManager;

	@ManagedProperty("#{moduleItemManager}")
	private IModuleItemManager moduleItemManager;

	@Bind(id = "uncheckedDataGrid", attribute = "dataProvider")
	public GridDataProvider getUnDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return moduleItemManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				List<ModuleItem> un = moduleItemManager.getAll();
				List<ModuleItem> ck = getSelectedMenu().getModuleItems();
				un.removeAll(ck);
				return un.toArray();
			}

			public int getTotalCount() {
				return moduleItemManager.getAll().size()
						- getSelectedMenu().getModuleItems().size();
			}
		};
	}

	@Bind(id = "checkedDataGrid", attribute = "dataProvider")
	public GridDataProvider getCkDataProvider() {
		return new GridDataProvider() {
			private static final long serialVersionUID = 1L;

			public Object[] getElementsById(String[] ids) {
				return moduleItemManager.getElementsById(ids).toArray();
			}

			public Object[] getElements() {
				return getSelectedMenu().getModuleItems().toArray();
			}

			public int getTotalCount() {
				return getSelectedMenu().getModuleItems().size();
			}
		};
	}

	private Menu getSelectedMenu() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
		return menuManager.get(selectedModuleId);
	}

	@Action(id = "del")
	public void del() {
		selectedMenu = getSelectedMenu();
		Object[] mis = checkedDataGrid.getSelectedValues();
		if (mis == null || mis.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object moduleItem : mis) {
				selectedMenu.getModuleItems().remove((ModuleItem) moduleItem);
			}
			menuManager.save(selectedMenu);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
		}
	}

	@Action(id = "add")
	public void add() {
		selectedMenu = getSelectedMenu();
		Object[] mis = uncheckedDataGrid.getSelectedValues();
		if (mis == null || mis.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录！');");
			return;
		} else {
			for (Object moduleItem : mis) {
				selectedMenu.getModuleItems().add((ModuleItem) moduleItem);
			}
			menuManager.save(selectedMenu);
			checkedDataGrid.setSelections(new int[] { -1 });
			checkedDataGrid.reload();
			uncheckedDataGrid.setSelections(new int[] { -1 });
			uncheckedDataGrid.reload();
		}
	}

	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();
	}
}
